Skip to content

deezer: fall back to MP3 when the server raises a country (geolocation) error - #1045

Open
slannez wants to merge 1 commit into
nathom:devfrom
slannez:fix/deezer-geolocation-mp3-fallback
Open

slannez wants to merge 1 commit into
nathom:devfrom
slannez:fix/deezer-geolocation-mp3-fallback

Conversation

@slannez

@slannez slannez commented Nov 2, 2026

Copy link
Copy Markdown

Problem

When downloading from Deezer, the media endpoint (media.deezer.com/v1/get_url) sometimes rejects the track URL request with error code 2002, which deezer-py surfaces as WrongGeolocation ("can't be streamed in country X"). In practice this restriction usually only applies to the lossless (FLAC) master — the MP3_320/MP3_128 formats of the same track are typically still available in the user's country.

DeezerClient.get_downloadable only retried with the FALLBACK track id at the same quality and otherwise raised NonStreamableError, so tracks were skipped (see #806) even though an MP3 version could be downloaded.

Fix

In streamrip/client/deezer.py, on deezer.WrongGeolocation the same track is now retried at one quality lower (FLAC -> MP3_320 -> MP3_128) when lower_quality_if_not_available is enabled (default: true), before falling back to the FALLBACK track id. NonStreamableError is raised only when even MP3_128 is geoblocked and no fallback track exists.

The resulting DeezerDownloadable gets the right extension (mp3 for quality <= 1) and size automatically, since it derives them from the resolved quality.

Tests

Added unit tests in tests/test_deezer.py covering:

  • FLAC geoblocked -> falls back to MP3_320 (quality == 1, .mp3)
  • FLAC + MP3_320 geoblocked -> falls back to MP3_128 (quality == 0)
  • all formats geoblocked, no fallback track -> NonStreamableError
  • all formats geoblocked, FALLBACK id present -> downloads the fallback track as MP3
  • lower_quality_if_not_available = false -> no quality fallback, raises immediately

pytest tests/test_deezer.py: 9 passed, 1 skipped (integration test needs DEEZER_ARL). Full suite: only pre-existing unrelated failure in tests/test_meta.py::test_album_metadata_qobuz (also fails on main).

When the Deezer media endpoint rejects a track URL request with error
code 2002 (WrongGeolocation), the restriction usually only applies to
the lossless (FLAC) master; the MP3_320/MP3_128 formats of the same
track are often still available in the user's country.

Previously, get_downloadable only retried with the FALLBACK track id at
the same quality and otherwise raised NonStreamableError, so tracks
were skipped even though an MP3 version could be downloaded.

Now, on WrongGeolocation the same track is retried at one quality
lower (FLAC -> MP3_320 -> MP3_128) when lower_quality_if_not_available
is enabled (the default). The FALLBACK track is only tried once even
MP3_128 is geoblocked, and NonStreamableError is raised only when
nothing is available.

Fixes nathom#806

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant